const u = navigator.userAgent;
// const app = navigator.appVersion;
export const  isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
export const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

interface WebViewJavascriptBridgeInterface {
  callHandler:Function,
  registerHandler:Function,
}

declare global {
  interface Window {
    WebViewJavascriptBridge:WebViewJavascriptBridgeInterface,
    WVJBCallbacks: Function[]
  }
}


function setupWebViewJavascriptBridge(callback:Function) {
  if (window.WebViewJavascriptBridge) {
    return callback(window.WebViewJavascriptBridge);
  }
  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback);
  }
  window.WVJBCallbacks = [callback];
  var WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'https://__bridge_loaded__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(function() {
    document.documentElement.removeChild(WVJBIframe);
  }, 0);
}
if (isIOS) {
  setupWebViewJavascriptBridge(function() {
    console.log('ios init')
  });
}

/**
 * 禁用下拉刷新
 * @param {*} data 0 禁用 1 开启  默认开启
 */
export const enableRefresh = (data = 0) => {
  try {
    window.WebViewJavascriptBridge.callHandler('enableRefresh', data);
  } catch (e) {
    console.log(e);
  }
};

/**
 *
 * @param {*} icon
 * @param {*} visible 原来的客户端按钮是否可见 1 可以见 0 隐藏
 * @param {*} cb
 */
export const handleButtons = (icon:any, visible = 0, cb:Function) => {
  try {
    var data = {
      copyButtonVisible: visible,
      customIconUrl: icon,
    };
    window.WebViewJavascriptBridge.registerHandler('rightIconCb', function() {
      cb && cb();
    });
    console.log('register callback', window.WebViewJavascriptBridge);

    window.WebViewJavascriptBridge.callHandler('handleButtons', data);
  } catch (e) {
    console.log(e);
  }
};
/**
 * 充值
 */
export const chargeMbi = ()=>{
  let schemeUrl = "huyakeke";
  location.href = `${schemeUrl}://wallet?cmd=enter_wallet&from=${encodeURIComponent(
    location.href
  )}`;
}

function androidInit(bridge:any) {
  try {
    bridge.init(function(message:string, responseCallback:Function) {
      console.log('JS got a message', message);
      var data = {
        'Javascript Responds': '测试中文!',
      };
      if (responseCallback) {
        console.log('JS responding with', data);
        responseCallback(data);
      }
    });
  } catch (e) {
    console.log(e);
  }
}

function connectWebViewJavascriptBridge(callback:Function) {
  if (window.WebViewJavascriptBridge) {
    androidInit(window.WebViewJavascriptBridge);
    callback(window.WebViewJavascriptBridge);
  } else {
    document.addEventListener(
      'WebViewJavascriptBridgeReady',
      function() {
        androidInit(window.WebViewJavascriptBridge);

        callback(window.WebViewJavascriptBridge);
      },
      false
    );
  }
}

/**
 * 立即执行的 初始化设置
 * @param {} ready
 */
export const initClient = connectWebViewJavascriptBridge;
